Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming


Errors within a method

If a method needs to identify that it was unable to perform its action, the method can inform the caller by using an output or return parameter, setting a public data member, or using some other mechanism. For example, if the method has a return value (it's not a VOID method) the method might set the return value to the Unknown value (?), to indicate a failure. The caller of the method can test for this value and respond appropriately.

If a statement within the method raises ERROR (for example, a FIND statement fails) the existing 4GL error functionality occurs. This means that if the statement has NO-ERROR specified for it, the error message is passed to the ERROR-STATUS system handle and the next statement executes. The application can use the ERROR-STATUS system handle to determine the error that occurred and respond appropriately. If the statement does not have NO-ERROR specified for it, Progress displays the error message to the default output device and breaks out of the inner most block, conforming to the options specified in any ON ERROR phrase associated with the block.

The following examples show how a method from some class can propagate a failure status to its caller and respond to a statement raising ERROR:

Test.cls
METHOD PRIVATE VOID findCustomerAddress (INPUT iName AS CHAR,  
                                 OUTPUT oAddress AS CHAR): 
           oAddress = ?. 
/* 1 */    FIND customer WHERE customer.NAME BEGINS iName NO-ERROR. 
/* 2 */    IF AVAILABLE (customer) THEN 
               oAddress = customer.Address. 
END METHOD. 

Caller.p
        DEFINE VARIABLE rTest AS CLASS Test NO-UNDO. 
        rTest = NEW test ( ). 
        rTest:findCustomerAddress (INPUT "fred", OUTPUT szAddress) NO-ERROR. 
/* 3 */ IF szAddress NE ? THEN MESSAGE "fred " szAddress. 

The following numbered notes apply to the numbered code in the previous examples, Test.cls and Caller.p:

  1. If the FIND statement fails to locate a record, it raises ERROR and writes an error message to the ERROR-STATUS system handle.
  2. Note: If the FIND statement did not have NO-ERROR specified, an error in the statement would cause any enclosing DO block in the caller to end. If this happened, oAddress, and hence szAddress, could not be set and handled as expected, but this example handles it.

  3. The application checks to see if the record was found and sets the output value appropriately. If a record is not found, oAddress remains set to the Unknown value (?) to indicate an error.
  4. In the caller, either an error raised by the FIND statement or missing data in customer sets oAddress to the Unknown value (?) that signals the caller through szAddress to proceed accordingly.

Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095